package com.lbs.dao.Impl;

import com.lbs.dao.UserDao;
import com.lbs.entity.User;
import com.lbs.utils.BaseDao;

import java.util.List;

/**
 * @author Administrator
 * @date 2021/5/29 13:54
 * @description
 **/
public class UserDaoImpl extends BaseDao<User> implements UserDao {
    @Override
    public User findByUsernameAndPassword(String username, String password) {
        String sql = "select * from user where username = ? and password = ?";
        return findOne(sql,User.class,username,password);
    }

    @Override
    public int insertUser(User user) {
        String sql = "insert into user values(null,?,?,?,?,?,?,?,now(),?,null)";
        Object[] params = {user.getName(),user.getPhone(),user.getAge(),user.getSex(),
        user.getUsername(),user.getPassword(),user.getStatus(),user.getRole()};
        return update(sql,params);
    }

    @Override
    public List<User> selectAllByName(String sql,Object...param) {
        return findMore(sql,User.class,param);
    }

    /**
     * 批量删除
     * @param uid
     * @return
     */
    @Override
    public int deleteUserById(String uid) {
        String sql = "delete from user where uid = ?";
        return update(sql,uid);
    }

    @Override
    public int updateUserById(User user) {
        String sql = "update user set name = ?,phone = ?," +
                "age = ?,sex = ?,username = ?,password = ?,status = ?," +
                "createtime = now(),role = ? where uid = ?";
        Object[] params = {user.getName(),user.getPhone(),user.getAge(),user.getSex(),
                user.getUsername(),user.getPassword(),user.getStatus(),user.getRole(),user.getUid()};
        return update(sql,params);
    }

    /**
     * 根据uid查询用户信息
     * @param uid
     * @return
     */
    @Override
    public User findUserById(int uid) {
        String sql = "select * from user where uid = ?";
        return findOne(sql,User.class,uid);
    }
}
